<?php 
	if(isset($_POST['category']) && $_POST['category']!='') $post_category = $_POST['category'];
	else $post_category = '';
?>
<h2 class='header'>Blog Standings</h2>
<div class='contentBody'>
	<form action='standings' method='post'>
		<select name='category'>
		<?php 
			$db->Query("SELECT * FROM category;");
			while($db->getRow()) {
				echo "<option value='".$db->access[0]."' "; if($db->access[0]==$post_category) echo "selected='selected'"; echo ">".$db->access[1]."</option>";
			}
		?>
		</select>	<input type='submit' name='submit' value='Submit' />
	</form>
	<?php
		if($post_category) {
			$results = array();
			$db->Query("SELECT category.name, contestant.url, nomination.id, nomination.nomination_count, nomination.vote_count
										FROM category, contestant, nomination
										WHERE category.id = nomination.category_id AND contestant.id = nomination.contestant_id AND category.id = '".$post_category."';");
			$nominations = $db->getResults();
			foreach($nominations as $nom) {
				$db->Query("SELECT count(*) FROM member_vote WHERE nomination_id = '".$nom[2]."';");
				if($db->getRow()) $member_vote_count = $db->access[0]; $member_vote_count = 0;
				$db->Query("SELECT count(*) FROM user_vote, user WHERE user.id = user_vote.user_id AND user.celeb = '1' AND user_vote.nomination_id = '".$nom[2]."';");
				if($db->getRow()) $celeb_vote_count = $db->access[0]; $celeb_vote_count = 0;
				$db->Query("SELECT count(*) FROM user_vote, user WHERE user.id = user_vote.user_id AND user.celeb = '0' AND user_vote.nomination_id = '".$nom[2]."';");
				if($db->getRow()) $public_vote_count = $db->access[0]; $public_vote_count = 0;
				$weighting = ($member_vote_count*0.1) + ($celeb_vote_count*0.3) + ($public_vote_count*0.5);
				$results[] = array ('category_name' => $nom[0], 
														'contestant_url' => $nom[1], 
														'nomination_count' => $nom[3], 
														'total_vote_count' => $nom[4], 
														'judge_votes' => $member_vote_count, 
														'celeb_votes' => $celeb_vote_count, 
														'public_votes' => $public_vote_count,
														'weighting' => $weighting);
			}

			foreach ($results as $key => $row) {
					$a_category_name[$key] = $row['category_name'];
					$a_contestant_url[$key] = $row['contestant_url'];
					$a_nomination_count[$key] = $row['nomination_count'];
					$a_total_vote_count[$key] = $row['total_vote_count'];
					$a_judge_votes[$key] = $row['judge_votes'];
					$a_celeb_votes[$key] = $row['celeb_votes'];
					$a_public_votes[$key] = $row['public_votes'];
					$a_weighting[$key] = $row['weighting'];
			}

			array_multisort($a_weighting, SORT_DESC, $results);
			
			$count = 1;
			echo "<table border='1' cellspacing='0' cellpadding='5'>
							<tr>
								<th>Position</th>
								<th>Blog</th>
								<th>Nominations</th>
								<th>Total Votes</th>
								<th>Judges Votes</th>
								<th>Celeb Voted</th>
								<th>Public Votes</th>
								<th>Weighting</th>
							</tr>";
			foreach ($results as $key => $row) {
				echo "<tr>
								<td>".$count."</td>
								<td>".$row['contestant_url']."</td>
								<td>".$row['nomination_count']."</td>
								<td>".$row['total_vote_count']."</td>
								<td>".$row['judge_votes']."</td>
								<td>".$row['celeb_votes']."</td>
								<td>".$row['public_votes']."</td>
								<td>".$row['weighting']."</td>
							</tr>";
				$count++;
			}
			echo "</table>";
		} else echo "<p>Please select the category to view.</p>";
	?>
</div>